package com.bitmain.mmst.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bitmain.mmst.common.consts.ResultCode.ERROR;
import com.bitmain.mmst.common.consts.ResultCode.INFO;
import com.bitmain.mmst.core.struct.APIResult;
import com.bitmain.mmst.core.struct.ServiceResult;
import com.bitmain.mmst.domain.MineHashrateOverview;
import com.bitmain.mmst.web.service.OverviewService;

/**
 *
 * @author Richard - rui.wu@bitmain.com
 * @since 2016年8月23日
 * 
*/

@Controller
@RequestMapping("overview")
public class OverviewController {

	@Autowired
	private OverviewService overviewService;
	
	@RequestMapping(value="hashrate",method=RequestMethod.GET)
	public ModelAndView hashrate(){
		ModelAndView mv = new ModelAndView("overview/hashrate");
		return mv;
	}
	
	@RequestMapping(value="hashrates/latest",method=RequestMethod.GET)
	@ResponseBody
	public APIResult latest(HttpServletRequest request){
		System.out.println("进入hashrates/latest的控制器");
		APIResult result = new APIResult();
		ServiceResult<MineHashrateOverview> rs = overviewService.fetchLatestHashrateOverview();
	
		if(BooleanUtils.isTrue(rs.getSuccess())){
			result.setCode(INFO.SUCCESS_QUERY);
			result.setSuccess(true);
			result.setData(rs.getData());
		}else{
			result.setCode(ERROR.DB_DELETE);
			result.setSuccess(true);
		}
		return result;
	}
}

